﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using YogaStudio.Data;
using YogaStudio.Data.Domain;
using YogaStudio.Service.BLL;
using YogaStudio.WebUI.Models;

namespace YogaStudio.WebUI.Controllers
{
    public class WaiverRequestController : Controller
    {
        IFacultyService facultyService = new FacultyService();
        IWaiverService waiverService = new WaiverService();
        ICourseService courseService = new CourseService();
        ICustomerService customerService = new CustomerService();

        // GET: WaiverRequest
        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles="Faculty")]
        public ActionResult AllWaivers()
        {
            var faculty = facultyService.GetFacultyByLogInUserName(User.Identity.Name);
            if (faculty == null)
            {
                //return RedirectToAction("/Faculty/");
                return new HttpStatusCodeResult(HttpStatusCode.MethodNotAllowed);
            }
            var waivers = facultyService.GetWaiverRequestsByFacultyId(faculty.Id);
            return View(new AllWaiversViewModel 
            {
                WaiverRequests = waivers.ToList(),
                Memo = faculty.FullName,
            });
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ViewBag.Id = id.Value;
            ViewBag.IsIdValid = waiverService.IsWaiverRequestIdValid(id.Value);
            return View();
        }

        public ActionResult _Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var waiver = waiverService.GetWaiverRequestById(id.Value);
            if (waiver == null)
            {
                return HttpNotFound();
            }
            return PartialView(waiver);
        }

        [Authorize(Roles="Faculty")]
        public ActionResult ReqChgWaiverStatus(int id, WaiverRequestStatus status)
        {
            ViewBag.Id = id;
            ViewBag.Status = status;
            var waiver = waiverService.GetWaiverRequestById(id);
            var faculty = facultyService.GetFacultyByLogInUserName(User.Identity.Name);
            if (waiver.FacultyId != faculty.Id)
            {
                return  new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
            }
            return View(waiver);
        }

        [Authorize(Roles="Faculty")]
        [HttpPost]
        public ActionResult ReqChgWaiverStatus([Bind(Include="Id, Status, ResponseReason")]WaiverRequest waiver)
        {
            if (ModelState.IsValid)
            {
                waiverService.UpdateWaiverStatus(waiver);
            }
            return RedirectToAction("AllWaivers");
        }

        [Authorize(Roles="Customer")]
        public ActionResult RequestWaiver()
        {
            var courses = courseService.GetAllCourses();
            var name = User.Identity.Name;

            Customer customer = customerService.GetCustomerByLogInUserName(User.Identity.Name);

            //var advisorOfCustomer = customer.Advisor;
            var waiverRequest = new WaiverRequest
            {
                Faculty = customer.Advisor,
                Customer = customer
            };

            return View(new RequestWaiver
                {
                    WaiverRequest = waiverRequest,
                    Courses = courses
                });
        }

        [HttpPost]
        public ActionResult RequestWaiver(RequestWaiver rw)
        {
            var waiver = new WaiverRequest
            {
                CustomerId = rw.WaiverRequest.Customer.Id,
                FacultyId = rw.WaiverRequest.Faculty.Id,
                CourseId = rw.WaiverRequest.CourseId,
                Reason = rw.WaiverRequest.Reason
            };
            waiverService.CreateNewWaiver(waiver);

            return RedirectToAction("mywaiverrequest");
        }

        public ActionResult MyWaiverRequest()
        {
            var name = User.Identity.Name;

            Customer customer = customerService.GetCustomerByLogInUserName(User.Identity.Name);
            var waivers = waiverService.GetAllWaiverRequest(customer.Id).ToList();
            return View(waivers);
            //var waiverViewModel = from waiver in waivers
            //                      select new RequestWaiver
            //                      {

            //                          WaiverRequest = waiver
            //                      };

            //return View(new WaiverRequest
            //{
            //    WaiverRequest = waivers.ToList()
            //});
        }

        protected override void Dispose(bool disposing)
        {
            facultyService.Dispose(disposing);
            customerService.Dispose(disposing);
            courseService.Dispose(disposing);
            waiverService.Dispose(disposing);
            base.Dispose(disposing);
        }
    }
}